/**
 * author: zhangwei
 * 角色管理列表
 */
status = ""
var main = {

    init: function() {
        this.initTable();
        this.tabBind();
    },
    initTable: function() {
        var _this=this;
        loadTable();

        // 添加
        $('#btnAdd').click(function() {
            localStorage.studentType = "add";
            parent.$t.goToPage(this, "/student/manage");

        });

        // 添加
        $('#downloadTemplate').click(function() {
            window.open("/template/student.xlsx")
        });
    },
    tabBind: function() {
        layui.use(['form','upload'], function () {
            var form = layui.form;
            form.on('submit(formDemo)', function(data){
                loadTable();
                return false;
            });

            form.on('select(status)', function(data){
                status = data.value
                return false;
            });

            var upload = layui.upload;

            var uploadInst = upload.render({
                elem: '#upload' //绑定元素
                ,url: '/student/upload/' //上传接口
                ,accept:'file'
                ,done: function(res){
                    successMsg("导入成功");
                    loadTable()
                }
                ,error: function(){
                    //请求异常回调
                    successMsg("导入失败");
                }
            });

        });
    }
}
main.init();

function loadTable() {
    layui.use('table', function(){
        var table = layui.table;
        var form = layui.form;
        var college = $("#college").val()
        var professionalName = $("#professionalName").val()
        var studentName = $("#studentName").val()
        table.render({
            elem: '#test'
            ,url:"/student/queryStudentPage?college="+college+"&professional="+professionalName
            +"&studentName="+studentName
            ,title: '数据表'
            ,cols: [[
                {type:'numbers', title:'序号', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'idCard', width:200, title:'身份证号'}
                ,{field:'studentCode', title:'学号'}
                ,{field:'studentName', title:'学生名称'}
                ,{field:'gender', title:'性别'}
                ,{field:'brithday', title:'出生日期', templet: function (res) {
                        return formatSimpleDate(res.birthday);
                    }}
                ,{field:'province', title:'省份'}
                ,{field:'college', title:'所在学院'}
                ,{field:'professional', title:'专业'}
                ,{field:'level', title:'层次'}
                ,{field:'className', title:'班级'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:200}
            ]]
            ,page: true
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del') {
                layer.confirm('您确定要删除么', function(index){
                    var json = {"id":data.id};
                    $.ajax({
                        type:"get",
                        data:json,
                        async:false,
                        url:"/student/deleteStudent",
                        success:function(result) {
                            if (result.code == property.successCode) {
                                successMsg("删除成功");
                                loadTable();
                            } else{
                                errorMsg(result.msg);
                            }
                        },
                        error:function(result) {
                            errorMsg("系统异常");
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit'){
                localStorage.studentType = "edit";
                localStorage.studentId = data.id;
                parent.$t.goToPage(this, "/student/manage");
            }
        });
    });
}

